# 写入yaml文件
import os

import yaml

# 获取到的是handles_yaml.py文件的绝对路径
# env_yaml_path
# 项目文件的路径
project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# env_vars.yaml文件的路径
env_yaml_path = project_path + "/configs/env_vars.yaml"


# print(env_yaml_path)


# 读取env_vars.yaml
def read_vars1(key):
    with open(env_yaml_path, mode="r", encoding="utf-8") as f:
        res = yaml.safe_load(f)
    if key:
        return res[key]
    else:
        return res


# 写入yaml文件  （更新yaml文件的内容）
def write_var(dict_1: dict):
    res = read_vars1(None)  # {'变量名称1': '变量值1', '变量名称2': '变量值2'}
    # dict_1 = {"name": "文君","token":"xxxxxxx"}
    if res is None:
        res = {}
    res.update(dict_1)
    # print(res)
    with open(env_yaml_path, mode="w", encoding="utf-8") as f:
        yaml.safe_dump(res, f, allow_unicode=True)  # 转成字符，也可以写入到文件


# 读取测试用例数据
def read_testcase_info(yaml_path):
    with open(yaml_path, mode="r", encoding="utf-8") as f:
        res = yaml.safe_load(f)
        return res



if __name__ == '__main__':
    pass
    print(read_vars1('pass_list'))
    # write_var({"age": 18})
    # read_testcase_info()
